package com.skill.shareskill.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.skill.shareskill.R;
import com.skill.shareskill.fragment.FindFragment;
import com.skill.shareskill.fragment.MainFragment;
import com.skill.shareskill.fragment.MyFragment;
import com.skill.shareskill.fragment.NewsFragment;
import com.skill.shareskill.fragment.ReleaseFragment;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    private FrameLayout fl_container;

    private LinearLayout ll_main;
    private LinearLayout ll_find;
    private LinearLayout ll_release;
    private LinearLayout ll_news;
    private LinearLayout ll_my;

    private ImageView iv_main;
    private TextView tv_main;
    private ImageView iv_find;
    private TextView tv_find;
    private ImageView iv_release;
    private ImageView iv_news;
    private TextView tv_news;
    private ImageView iv_my;
    private TextView tv_my;

    MainFragment mainFragment=new MainFragment();
    FindFragment findFragment=new FindFragment();
    ReleaseFragment releaseFragment=new ReleaseFragment();
    NewsFragment newsFragment=new NewsFragment();
    MyFragment myFragment=new MyFragment();

    //RecyclerViewFragment recyclerViewFragment=new RecyclerViewFragment();
    //TestFragment testFragment=new TestFragment();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        ActionBar actionBar=getActionBar();
//        actionBar.hide();//隐藏标题栏

        setContentView(R.layout.activity_main);
        fl_container=findViewById(R.id.fl_container);

        ll_main=findViewById(R.id.ll_main);
        ll_main.setOnClickListener(this);
        ll_find=findViewById(R.id.ll_find);
        ll_find.setOnClickListener(this);
        ll_release=findViewById(R.id.ll_release);
        ll_release.setOnClickListener(this);
        ll_news=findViewById(R.id.ll_news);
        ll_news.setOnClickListener(this);
        ll_my=findViewById(R.id.ll_my);
        ll_my.setOnClickListener(this);

        iv_main=findViewById(R.id.iv_main);
        tv_main=findViewById(R.id.tv_main);

        iv_find=findViewById(R.id.iv_find);
        tv_find=findViewById(R.id.tv_find);

        iv_release=findViewById(R.id.iv_release);

        iv_news=findViewById(R.id.iv_news);
        tv_news=findViewById(R.id.tv_news);

        iv_my=findViewById(R.id.iv_my);
        tv_my=findViewById(R.id.tv_my);
        //changeFragment(recyclerViewFragment,"recyclerViewFragment");

       changeFragment(mainFragment,"mainFragment");
    }

    @Override
    public void onClick(View view) {
        iv_main.setImageResource(R.drawable.tab_icon_main_dark);
        tv_main.setTextColor(this.getResources().getColor(R.color.tab_text_dark));

        iv_find.setImageResource(R.drawable.tab_icon_find_dark);
        tv_find.setTextColor(this.getResources().getColor(R.color.tab_text_dark));

        iv_release.setImageResource(R.drawable.tab_icon_add_dark);

        iv_news.setImageResource(R.drawable.tab_icon_news_dark);
        tv_news.setTextColor(this.getResources().getColor(R.color.tab_text_dark));

        iv_my.setImageResource(R.drawable.tab_icon_me_dark);
        tv_my.setTextColor(this.getResources().getColor(R.color.tab_text_dark));

        hideFragments(getSupportFragmentManager().beginTransaction());
        switch (view.getId()){
            case R.id.ll_main:{
                mainFragment=new MainFragment();
                changeFragment(mainFragment,"mainFragment");
                //changeFragment(recyclerViewFragment,"recyclerViewFragment");
                iv_main.setImageResource(R.drawable.tab_icon_main_light);
                tv_main.setTextColor(this.getResources().getColor(R.color.tab_text_light));
                break;
            }
            case R.id.ll_find:{
                changeFragment(findFragment,"findFragment");
                //changeFragment(testFragment,"testFragment");
                iv_find.setImageResource(R.drawable.tab_icon_find_light);
                tv_find.setTextColor(this.getResources().getColor(R.color.tab_text_light));
                break;
            }
            case R.id.ll_release:{
                changeFragment(releaseFragment,"releaseFragment");
                iv_release.setImageResource(R.drawable.tab_icon_add_light);
                break;
            }
            case R.id.ll_news:{
                changeFragment(newsFragment,"newsFragment");
                iv_news.setImageResource(R.drawable.tab_icon_news_light);
                tv_news.setTextColor(this.getResources().getColor(R.color.tab_text_light));
                break;
            }
            case R.id.ll_my:{
                changeFragment(myFragment,"myFragment");
                iv_my.setImageResource(R.drawable.tab_icon_me_light);
                tv_my.setTextColor(this.getResources().getColor(R.color.tab_text_light));
                break;
            }
        }
    }
    private void changeFragment(Fragment fragment,String tag){
        FragmentManager fm=this.getSupportFragmentManager();
        FragmentTransaction ft=fm.beginTransaction();
        ft.replace(R.id.fl_container,fragment,tag);
        ft.commit();
    }
    /**
     *
     * TODO 隐藏所有的fragment，防止多个fragment显示在界面
     *
     */
    private void hideFragments(FragmentTransaction transaction) {
        if (mainFragment != null) {
            transaction.hide(mainFragment);
        }
        if (findFragment != null) {
            transaction.hide(findFragment);
        }
        if(releaseFragment!=null){
            transaction.hide(releaseFragment);
        }
        if(newsFragment!=null){
            transaction.hide(newsFragment);
        }
        if (myFragment != null) {
            transaction.hide(myFragment);
        }
    }
}
